package view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import com.toedter.calendar.JDateChooser;

import dao.DAOFactory;
import dao.IClienteDAO;

import model.Cliente;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ClienteView extends JFrame {

	private JPanel contentPane;
	private JTextField textNome;
	private JButton btnSalvar;
	private JDateChooser chooser;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClienteView frame = new ClienteView();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ClienteView() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNome = new JLabel("Nome:");
		lblNome.setBounds(46, 56, 46, 14);
		contentPane.add(lblNome);
		
		textNome = new JTextField();
		textNome.setBounds(92, 53, 86, 20);
		contentPane.add(textNome);
		textNome.setColumns(10);
		
		btnSalvar = new JButton("Salvar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Cliente c = new Cliente();
				c.setNome( textNome.getText() );
				c.setDtNascimento( chooser.getDate() );
				
//				IClienteDAO dao = new ClienteDAO();
				IClienteDAO dao = DAOFactory.getClienteDAO();
				if ( dao.insert( c ) ) {
					JOptionPane.showMessageDialog(
							getParent(), "Dados inseridos",
							"Adicionado ao banco",
							JOptionPane.WARNING_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(
							getParent(), "Erro",
							"Erro",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnSalvar.setBounds(46, 190, 89, 23);
		contentPane.add(btnSalvar);
		
		chooser = new JDateChooser();
		chooser.setSize(167, 20);
		chooser.setLocation(92, 95);
		contentPane.add(chooser);
		
		JLabel lblData = new JLabel("Data");
		lblData.setBounds(29, 95, 46, 14);
		contentPane.add(lblData);
	}
}
